package com.inossem.allotin.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.allotin.R;
import com.inossem.allotin.adapter.input.MyViewPageAdapter;
import com.inossem.allotin.adapter.input.SubmitDialogPicAdapter;
import com.inossem.allotin.api.LogInApiService;
import com.inossem.allotin.api.TransportInputApiService;
import com.inossem.allotin.bean.login.LogInBean;
import com.inossem.allotin.bean.login.LogInRequestBean;
import com.inossem.allotin.bean.offLine.OffLineInfoBean;
import com.inossem.allotin.db.AllotInBean;
import com.inossem.allotin.fragment.TransportInputMaterialFragment;
import com.inossem.allotin.utils.ACacheUtils;
import com.inossem.allotin.utils.BaseDialog;
import com.inossem.allotin.utils.DialogUtils;
import com.inossem.allotin.utils.GreenDaoManager;
import com.inossem.allotin.utils.OffLineUtils;
import com.inossem.allotin.utils.PicSelectUtil;
import com.inossem.allotin.utils.TimeDialogUtil;
import com.inossem.allotin.utils.TimeUtils;
import com.inossem.allotin.utils.ToastUtils;
import com.inossem.allotin.utils.Utils;
import com.inossem.allotin.utils.requestUtils.InossemNewsCallback;
import com.inossem.allotin.utils.requestUtils.RetrofitUtils;
import com.inossem.allotin.view.HorizontalListView;
import com.inossem.wms.co.transport.TransportSubmitCo;
import com.inossem.wms.model.biz.BizReceiptAttachmentInfoCo;
import com.inossem.wms.model.biz.BizReceiptAttachmentVo;
import com.inossem.wms.model.file.LogFile;
import com.inossem.wms.vo.BaseObject;
import com.inossem.wms.vo.auth.DicWhStorageTypeTreeVo;
import com.inossem.wms.vo.auth.DicWhTreeVo;
import com.inossem.wms.vo.transport.TransportHead;
import com.inossem.wms.vo.transport.TransportItem;
import com.inossem.wms.vo.transport.TransportItemVo;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Response;

import static android.view.View.GONE;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
import static com.inossem.allotin.InossemApplication.getContext;
import static com.inossem.allotin.constant.ItemStatusConstant.DATA;
import static com.inossem.allotin.constant.ItemStatusConstant.FILE;
import static com.inossem.allotin.constant.ItemStatusConstant.LIST_STRING;
import static com.inossem.allotin.constant.ItemStatusConstant.MULTIPART_FORM_DATA;
import static com.inossem.allotin.constant.ItemStatusConstant.NULL;
import static com.inossem.allotin.constant.ItemStatusConstant.PURCHASE_INSPECTION;
import static com.inossem.allotin.constant.ItemStatusConstant.RECEIPT_CODE;
import static com.inossem.allotin.constant.ItemStatusConstant.RECEIPT_TYPE;
import static com.inossem.allotin.constant.ItemStatusConstant.RESPONSE;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_CLOSED;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_COMPLETED;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_DELETED;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_DRAFT;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_FOUR;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_ONE;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_ONE_STRING;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_POSTING;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_REJECTED;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_REQUESTCODE_1;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_SUBMITTED;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_SYNC;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_TASK;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_THREE;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_TWO;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_TWO_STRING;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_UNSYNC;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_WRITED_OFF;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_WRITING_OFF;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_ZERO;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_ZERO_STRING;
import static com.inossem.allotin.constant.ItemStatusConstant.STOCK_INPUT_CODE;
import static com.inossem.allotin.constant.ItemStatusConstant.WHETHER;
import static com.inossem.allotin.constant.ItemStatusConstant.YEAR_MONTH_DAY;
import static com.inossem.allotin.fragment.TransportInputMaterialFragment.CAN_EDIT;
import static com.inossem.allotin.utils.OffLineUtils.TRANSPORT_INPUT;
import static com.inossem.allotin.utils.TimeUtils.transitionStringToData;

/**
 * @author LinH
 */
public class TransportInputDetailsActivity extends AppCompatActivity implements View.OnClickListener {
    private String whether;
    private List<BizReceiptAttachmentVo> submitFileList = new ArrayList<>();
    private ViewPager vp;
    private ImageView iv;
    private TabLayout tlTitle;
    private HorizontalListView lv;
    private RelativeLayout rlSearch;
    private LinearLayout llInformation1, llInformation2;
    private TextView tvPost, tvSave, tvSubmit, tvSubmitPost, tvApproval, tv_delete, tvMoreInfo, tvSubtitleTop, tvSubtitleBottom, tvInformation1;
    private Gson gson;
    private Bundle bundle;
    private TransportInputMaterialFragment transportInputMaterialFragment;
    private TransportHead transportHead;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transport_input_details);
        //默认不弹出键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //强制横屏z
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        GreenDaoManager.getInstance().init(TransportInputDetailsActivity.this);
        initView();
        initData();
    }

    public void initView() {
        vp = findViewById(R.id.vp);
        tvPost = findViewById(R.id.tv_post);
        tvSave = findViewById(R.id.tv_save);
        tlTitle = findViewById(R.id.tl_title);
        rlSearch = findViewById(R.id.rl_search);
        tvSubmit = findViewById(R.id.tv_submit);
        tvApproval = findViewById(R.id.tv_approval);
        tv_delete = findViewById(R.id.tv_delete);
        tvMoreInfo = findViewById(R.id.tv_more_info);
        TextView tvReturn = findViewById(R.id.tv_return);
        tvSubtitleTop = findViewById(R.id.tv_subtitle_top);
        tvSubmitPost = findViewById(R.id.tv_submit_post);
        llInformation1 = findViewById(R.id.ll_information_1);
        tvInformation1 = findViewById(R.id.tv_information_1);
        llInformation2 = findViewById(R.id.ll_information_2);
        tvSubtitleBottom = findViewById(R.id.tv_subtitle_bottom);
        findViewById(R.id.tv_save).setOnClickListener(this);
        findViewById(R.id.tv_post).setOnClickListener(this);
        findViewById(R.id.tv_return).setOnClickListener(this);
        findViewById(R.id.tv_submit).setOnClickListener(this);
        findViewById(R.id.tv_approval).setOnClickListener(this);
        findViewById(R.id.tv_more_info).setOnClickListener(this);
        findViewById(R.id.tv_submit_post).setOnClickListener(this);
        findViewById(R.id.tv_delete).setOnClickListener(this);
        vp.setOffscreenPageLimit(STATUS_ONE);
        Utils.setButtonWidth(tvReturn, (byte) STATUS_ONE);
    }

    public void initData() {
        gson = new Gson();
        bundle = new Bundle();
        transportHead = gson.fromJson(ACacheUtils.getOrderJson(), TransportHead.class);
        initFragment(transportHead.getInfo());
        ACacheUtils.removeOrderJson();
//        gson = new Gson();
//        bundle = getIntent().getExtras();
//        if (bundle != null) {
//            String data = bundle.getString(DATA);
//            String listString = bundle.getString(LIST_STRING);
//            whether = getIntent().getStringExtra(WHETHER);
//            transportHead = gson.fromJson(listString, TransportHead.class);
//            initFragment(transportHead.getInfo());
//        }
    }

    private void initFragment(TransportItemVo responses) {
        InitBundle initBundle = new InitBundle(responses).invoke();
        TransportItem vo = initBundle.getVo();
        ArrayList<String> list = initBundle.getList();
        ArrayList<Fragment> listFragment = initBundle.getListFragment();
        TransportItemVo response = initBundle.getResponse();
        boolean isCompile = initBundle.isCompile();
        if (TextUtils.isEmpty(vo.getStockTransportCode())) {
            tvSubtitleTop.setText(getString(R.string.common_create));
            tvSubtitleBottom.setText(getString(R.string.common_line_2));
        } else {
            tvSubtitleTop.setText(vo.getReceiptStatusName());
            tvSubtitleBottom.setText(vo.getStockTransportCode());
        }
        Byte receiptStatus = vo.getReceiptStatus();
        // 根据ReceiptStatus更改statusTv颜色,
        if (receiptStatus == STATUS_DRAFT) {
            tvSubtitleTop.setTextColor(ContextCompat.getColor(getContext(), R.color.base_green));
        } else if (receiptStatus == STATUS_SUBMITTED) {
            tvSubtitleTop.setTextColor(ContextCompat.getColor(getContext(), R.color.title_gray));
        } else if (receiptStatus == STATUS_POSTING) {
            tvSubtitleTop.setTextColor(ContextCompat.getColor(getContext(), R.color.title_gray));
        } else if (receiptStatus == STATUS_TASK) {
            tvSubtitleTop.setTextColor(ContextCompat.getColor(getContext(), R.color.base_prasinous));
        } else if (receiptStatus == STATUS_UNSYNC) {
            tvSubtitleTop.setTextColor(ContextCompat.getColor(getContext(), R.color.base_orange));
        } else if (receiptStatus == STATUS_SYNC) {
            tvSubtitleTop.setTextColor(ContextCompat.getColor(getContext(), R.color.title_gray));
        } else if (receiptStatus == STATUS_COMPLETED) {
            tvSubtitleTop.setTextColor(ContextCompat.getColor(getContext(), R.color.title_gray));
        } else if (receiptStatus == STATUS_WRITING_OFF) {
            tvSubtitleTop.setTextColor(ContextCompat.getColor(getContext(), R.color.title_gray));
        } else if (receiptStatus == STATUS_WRITED_OFF) {
            tvSubtitleTop.setTextColor(ContextCompat.getColor(getContext(), R.color.title_gray));
        } else if (receiptStatus == STATUS_DELETED) {
            tvSubtitleTop.setTextColor(ContextCompat.getColor(getContext(), R.color.title_gray));
        } else if (receiptStatus == STATUS_CLOSED) {
            tvSubtitleTop.setTextColor(ContextCompat.getColor(getContext(), R.color.title_gray));
        } else if (receiptStatus == STATUS_REJECTED) {
            tvSubtitleTop.setTextColor(ContextCompat.getColor(getContext(), R.color.base_red));
        }
        tvInformation1.setText(String.valueOf(response.getItemList().size()));
        //初始化Fragment
        list.add(getString(R.string.common_material_information));
        transportInputMaterialFragment = new TransportInputMaterialFragment();
        transportInputMaterialFragment.setArguments(bundle);
        listFragment.add(transportInputMaterialFragment);
        if (isCompile) {
            llInformation2.setVisibility(VISIBLE);
        } else {
            llInformation2.setVisibility(GONE);
        }
        Utils.setButtonWidth(tvSubmit, response.getButton().getButtonSubmit());
        Utils.setButtonWidth(tvSave, response.getButton().getButtonSave());
        Utils.setButtonWidth(tv_delete, (byte) 1);
//        Utils.setButtonWidth(tvPost, response.getButton().getButtonPost());
//        Utils.setButtonWidth(tvSubmitPost, response.getButton().getButtonSubmitPost());
        Utils.setButtonWidth(tvPost, (byte) 0);
        Utils.setButtonWidth(tvSubmitPost, (byte) 0);
        if (!TextUtils.isEmpty(whether)) {
            if (whether.equals(STATUS_ZERO_STRING)) {
                Utils.setButtonWidth(tvMoreInfo, (byte) STATUS_ONE);
                Utils.setButtonWidth(tvApproval, (byte) STATUS_ONE);
            } else if (whether.equals(STATUS_TWO_STRING)) {
                Utils.setButtonWidth(tvMoreInfo, (byte) STATUS_ONE);
                Utils.setButtonWidth(tvApproval, (byte) STATUS_ZERO);
            }
        } else {
            Utils.setButtonWidth(tvMoreInfo, (byte) STATUS_ZERO);
            Utils.setButtonWidth(tvApproval, (byte) STATUS_ZERO);
        }
        MyViewPageAdapter myViewPageAdapter = new MyViewPageAdapter(getSupportFragmentManager(), list, listFragment);
        myViewPageAdapter.setOnButtonClickListener((int i) -> {
            if (i == STATUS_ZERO) {
                rlSearch.setVisibility(VISIBLE);
                llInformation1.setVisibility(VISIBLE);
                llInformation2.setVisibility(VISIBLE);
            } else if (i == STATUS_ONE) {
                rlSearch.setVisibility(INVISIBLE);
                llInformation1.setVisibility(GONE);
                llInformation2.setVisibility(GONE);
            } else if (i == STATUS_TWO) {
                rlSearch.setVisibility(INVISIBLE);
                llInformation1.setVisibility(GONE);
                llInformation2.setVisibility(GONE);
            } else if (i == STATUS_THREE) {
                rlSearch.setVisibility(INVISIBLE);
                llInformation1.setVisibility(GONE);
                llInformation2.setVisibility(GONE);
            }
        });
        vp.setAdapter(myViewPageAdapter);
        tlTitle.setupWithViewPager(vp);
    }

    @Override
    public void onClick(View v) {
        Utils.closeSoftKeyboard(v);
        int j = v.getId();
        switch (j) {
            case R.id.tv_return:
                finish();
                break;
            case R.id.tv_save:
                // 保存
                TransportItemVo beanSave = transportInputMaterialFragment.getBean(false);
                BaseDialog dialogSave = BaseDialog.createDialog(TransportInputDetailsActivity.this, R.layout.dialog_inspect_notice_form, 0.6f, 0.6f, (context, view, dialog) -> {
                    TextView tv = view.findViewById(R.id.tv);
                    tv.setText(getString(R.string.common_do_you_save));
                    if (beanSave.getFileList() != null) {
                        List<BizReceiptAttachmentVo> pics = Utils.getPicListFromFiles(beanSave.getFileList());
                        iv = view.findViewById(R.id.iv);
                        lv = view.findViewById(R.id.lv);
                        if (pics.isEmpty()) {
                            iv.setVisibility(VISIBLE);
                            submitFileList = new ArrayList<>();
                        } else {
                            iv.setVisibility(GONE);
                            submitFileList = beanSave.getFileList();
                        }
                        iv.setOnClickListener(view1 ->
                                PicSelectUtil.selectPic(TransportInputDetailsActivity.this, STATUS_FOUR, STATUS_REQUESTCODE_1));
                        lv.setOnItemClickListener((parent, view1, position, id) ->
                                PicSelectUtil.selectPic(TransportInputDetailsActivity.this, STATUS_FOUR, STATUS_REQUESTCODE_1));
                        showPicsToSubmitDialog();
                    }
                    SimpleDateFormat formatter = new SimpleDateFormat(YEAR_MONTH_DAY);
                    TextView docDate = view.findViewById(R.id.docDate);
                    docDate.setText(Utils.systemData());
                    docDate.setOnClickListener(v1 -> {
                        TimeDialogUtil time = new TimeDialogUtil(TransportInputDetailsActivity.this, dialog, (year, mouth, day) -> docDate.setText(String.format("%1$d-%2$d-%3$d", year, mouth, day)));
                        time.showDialogPop();
                    });
                    if (beanSave.getItemList().get(STATUS_ZERO).getDocDate() != null) {
                        String docDates = formatter.format(beanSave.getItemList().get(STATUS_ZERO).getDocDate());
                        docDate.setText(docDates);
                    }
                    TextView postingDate = view.findViewById(R.id.postingDate);
                    postingDate.setText(Utils.systemData());
                    if (beanSave.getItemList().get(STATUS_ZERO).getPostingDate() != null) {
                        String postingDates = formatter.format(beanSave.getItemList().get(STATUS_ZERO).getPostingDate());
                        postingDate.setText(postingDates);
                    }
                    postingDate.setOnClickListener(v1 -> {
                        TimeDialogUtil time = new TimeDialogUtil(TransportInputDetailsActivity.this, dialog, (year, mouth, day) -> postingDate.setText(String.format("%1$d-%2$d-%3$d", year, mouth, day)));
                        time.showDialogPop();
                    });

                    EditText etRemark = view.findViewById(R.id.et_remark);
                    if (!TextUtils.isEmpty(beanSave.getRemark())) {
                        etRemark.setText(beanSave.getRemark());
                    } else {
                        etRemark.setText(getString(R.string.transport_info_module_transport_info));
                    }
                    view.findViewById(R.id.tv_1).setOnClickListener(view1 -> dialog.cancel());
                    view.findViewById(R.id.tv_2).setOnClickListener(view1 -> {
                        beanSave.setRemark(etRemark.getText().toString());
                        for (TransportItem transportItem : beanSave.getItemList()) {
                            transportItem.setDocDate(transitionStringToData(docDate.getText().toString()));
                            transportItem.setPostingDate(transitionStringToData(postingDate.getText().toString()));
                        }
                        List<TransportItem> data = beanSave.getItemList();
                        TransportSubmitCo co = new TransportSubmitCo();
                        co.setRemark(etRemark.getText().toString());
                        co.setReceiptType(beanSave.getItemList().get(STATUS_ZERO).getReceiptType());

                        co.setDocDate(transitionStringToData(docDate.getText().toString()));
                        co.setPostingDate(transitionStringToData(postingDate.getText().toString()));

                        co.setItemList(data);
                        //作业地点相关 放在台头
                        if (null != co.getItemList() && !co.getItemList().isEmpty()) {
                            TransportItem matBean = co.getItemList().get(0);
                            co.setInputWhReal(matBean.getInputWhReal());
                            co.setOutputWhReal(matBean.getOutputWhReal());
                        }
                        List<BizReceiptAttachmentInfoCo> l = new ArrayList<>();
                        if (submitFileList.size() <= STATUS_ZERO) {
                            BizReceiptAttachmentInfoCo vo = new BizReceiptAttachmentInfoCo();
                            vo.setFileCode(NULL);
                            vo.setBizReceiptAttachmentId(STATUS_ZERO);
                            vo.setReceiptType(STATUS_ZERO);
                            vo.setReceiptCode(NULL);
                            l.add(vo);
                        } else {
                            for (int i = STATUS_ZERO; i < submitFileList.size(); i++) {
                                BizReceiptAttachmentInfoCo b = new BizReceiptAttachmentInfoCo();
                                b.setBizReceiptAttachmentId(submitFileList.get(i).getBizReceiptAttachmentId());
                                b.setFileCode(submitFileList.get(i).getFileCode());
                                l.add(b);
                            }
                        }
                        co.setFileList(l);
                        co.setIsSubmit((byte) 0);
                        if (!TextUtils.isEmpty(co.getItemList().get(0).getStockTransportCode())) {
                            saveOffLineData(beanSave);
                            DialogUtils.getShowMsgDialog(TransportInputDetailsActivity.this, getString(R.string.common_offline_data_save_success), () -> {
                                setResult(999);
                                finish();
                            }).show();
                        }
                    });
                });
                if (!dialogSave.isShowing()) {
                    dialogSave.show();
                }
                break;
            case R.id.tv_submit:
                // 提交
                TransportItemVo beanSubmit = transportInputMaterialFragment.getBean(true);
                if (beanSubmit != null) {
                    BaseDialog dialogSubmit = BaseDialog.createDialog(TransportInputDetailsActivity.this, R.layout.dialog_inspect_notice_form, 0.6f, 0.6f, (context, view, dialog) -> {
                        TextView tv = view.findViewById(R.id.tv);
                        tv.setText(getString(R.string.common_do_you_sub));
                        if (beanSubmit.getFileList() != null) {
                            List<BizReceiptAttachmentVo> pics = Utils.getPicListFromFiles(beanSubmit.getFileList());
                            iv = view.findViewById(R.id.iv);
                            lv = view.findViewById(R.id.lv);
                            if (pics.isEmpty()) {
                                iv.setVisibility(VISIBLE);
                                submitFileList = new ArrayList<>();
                            } else {
                                iv.setVisibility(GONE);
                                submitFileList = beanSubmit.getFileList();
                            }
                            iv.setOnClickListener(view1 -> PicSelectUtil.selectPic(TransportInputDetailsActivity.this, STATUS_FOUR, STATUS_REQUESTCODE_1));
                            lv.setOnItemClickListener((parent, view1, position, id) -> PicSelectUtil.selectPic(TransportInputDetailsActivity.this, STATUS_FOUR, STATUS_REQUESTCODE_1));
                            showPicsToSubmitDialog();
                        }

                        SimpleDateFormat formatter = new SimpleDateFormat(YEAR_MONTH_DAY);
                        TextView docDate = view.findViewById(R.id.docDate);
                        docDate.setText(Utils.systemData());
                        docDate.setOnClickListener(v1 -> {
                            TimeDialogUtil time = new TimeDialogUtil(TransportInputDetailsActivity.this, dialog, (year, mouth, day) -> docDate.setText(String.format("%1$d-%2$d-%3$d", year, mouth, day)));
                            time.showDialogPop();
                        });
                        if (beanSubmit.getItemList().get(STATUS_ZERO).getDocDate() != null) {
                            String docDates = formatter.format(beanSubmit.getItemList().get(STATUS_ZERO).getDocDate());
                            docDate.setText(docDates);
                        }
                        TextView postingDate = view.findViewById(R.id.postingDate);
                        postingDate.setText(Utils.systemData());
                        if (beanSubmit.getItemList().get(STATUS_ZERO).getPostingDate() != null) {
                            String postingDates = formatter.format(beanSubmit.getItemList().get(STATUS_ZERO).getPostingDate());
                            postingDate.setText(postingDates);
                        }
                        postingDate.setOnClickListener(v1 -> {
                            TimeDialogUtil time = new TimeDialogUtil(TransportInputDetailsActivity.this, dialog, (year, mouth, day) -> postingDate.setText(String.format("%1$d-%2$d-%3$d", year, mouth, day)));
                            time.showDialogPop();
                        });

                        EditText etRemark = view.findViewById(R.id.et_remark);
                        if (!TextUtils.isEmpty(beanSubmit.getRemark())) {
                            etRemark.setText(beanSubmit.getRemark());
                        } else {
                            etRemark.setText(getString(R.string.transport_info_module_transport_info));
                        }
                        view.findViewById(R.id.tv_1).setOnClickListener(view1 -> dialog.cancel());
                        view.findViewById(R.id.tv_2).setOnClickListener(view1 -> {
                            TransportSubmitCo co = new TransportSubmitCo();
                            co.setRemark(etRemark.getText().toString());
                            co.setReceiptType(beanSubmit.getItemList().get(STATUS_ZERO).getReceiptType());

                            co.setDocDate(transitionStringToData(docDate.getText().toString()));
                            co.setPostingDate(transitionStringToData(postingDate.getText().toString()));

                            co.setItemList(beanSubmit.getItemList());

                            //作业地点相关 放在台头
                            if (null != co.getItemList() && !co.getItemList().isEmpty()) {
                                TransportItem matBean = co.getItemList().get(0);
                                co.setInputWhReal(matBean.getInputWhReal());
                                co.setOutputWhReal(matBean.getOutputWhReal());
                            }

                            List<BizReceiptAttachmentInfoCo> l = new ArrayList<>();
                            if (submitFileList.size() <= STATUS_ZERO) {
                                BizReceiptAttachmentInfoCo vo = new BizReceiptAttachmentInfoCo();
                                vo.setFileCode(NULL);
                                vo.setBizReceiptAttachmentId(STATUS_ZERO);
                                vo.setReceiptType(STATUS_ZERO);
                                vo.setReceiptCode(NULL);
                                l.add(vo);
                            } else {
                                for (int i = STATUS_ZERO; i < submitFileList.size(); i++) {
                                    BizReceiptAttachmentInfoCo b = new BizReceiptAttachmentInfoCo();
                                    b.setBizReceiptAttachmentId(submitFileList.get(i).getBizReceiptAttachmentId());
                                    b.setFileCode(submitFileList.get(i).getFileCode());
                                    l.add(b);
                                }
                            }
                            co.setFileList(l);
                            //提交并过账
                            co.setIsSubmit((byte) 2);
                            login(co);
                        });
                    });
                    if (!dialogSubmit.isShowing()) {
                        dialogSubmit.show();
                    }
                }
                break;
            case R.id.tv_more_info:
                // 更多信息
                TransportItemVo beanMoreInfo = transportInputMaterialFragment.getBean(false);
                BaseDialog dialogMoreInfo = BaseDialog.createDialog(TransportInputDetailsActivity.this, R.layout.dialog_inspect_notice_form, (context, view, dialog) -> {
                    TextView tv = view.findViewById(R.id.tv);
                    tv.setText(getString(R.string.common_more_info));
                    List<BizReceiptAttachmentVo> pics = Utils.getPicListFromFiles(beanMoreInfo.getFileList());
                    iv = view.findViewById(R.id.iv);
                    lv = view.findViewById(R.id.lv);
                    if (pics.isEmpty()) {
                        iv.setVisibility(VISIBLE);
                        submitFileList = new ArrayList<>();
                    } else {
                        iv.setVisibility(GONE);
                        submitFileList = beanMoreInfo.getFileList();
                    }
                    showPicsToSubmitDialog();
                    EditText etRemark = view.findViewById(R.id.et_remark);
                    etRemark.setClickable(false);
                    etRemark.setEnabled(false);
//                    LinearLayout l7 = view.findViewById(R.id.l7);
//                    l7.setVisibility(GONE);
                    etRemark.setText(beanMoreInfo.getRemark());

                    SimpleDateFormat formatter = new SimpleDateFormat(YEAR_MONTH_DAY);
                    TextView docDate = view.findViewById(R.id.docDate);
                    docDate.setText(Utils.systemData());
                    if (beanMoreInfo.getItemList().get(STATUS_ZERO).getDocDate() != null) {
                        String docDates = formatter.format(beanMoreInfo.getItemList().get(STATUS_ZERO).getDocDate());
                        docDate.setText(docDates);
                    }
                    TextView postingDate = view.findViewById(R.id.postingDate);
                    postingDate.setText(Utils.systemData());
                    if (beanMoreInfo.getItemList().get(STATUS_ZERO).getPostingDate() != null) {
                        String postingDates = formatter.format(beanMoreInfo.getItemList().get(STATUS_ZERO).getPostingDate());
                        postingDate.setText(postingDates);
                    }

                });
                if (!dialogMoreInfo.isShowing()) {
                    dialogMoreInfo.show();
                }
                break;
            case R.id.tv_delete:
                DialogUtils.getMsgDialog(TransportInputDetailsActivity.this, new DialogUtils.OnClickListener() {
                            @Override
                            public void onClickListener() {
                                GreenDaoManager.getInstance().deleteAllotInBean(transportHead.getStockTransportCode());
                                ToastUtils.showToast(getString(R.string.common_delete_successful));
                                setResult(999);
                                finish();
                            }
                        }

                ).show();
                break;
            default:
                break;
        }
    }

    //登录
    private void login(TransportSubmitCo co) {
        LogInRequestBean requestBean = new LogInRequestBean();
        requestBean.setUserCode("adminsc");
        requestBean.setPassword("123456");
        RetrofitUtils.getRetrofit(TransportInputDetailsActivity.this, getString(R.string.developer_zhanjianyu), getString(R.string.developer_maoyu), getString(R.string.login_module_login), "登录")
                .create(LogInApiService.class)
                .login(requestBean)
                .enqueue(new InossemNewsCallback<BaseObject<LogInBean>>() {
                    @Override
                    public void success(Response<BaseObject<LogInBean>> response) {
                        if (!response.body().getData().getCode().equals("0")) {
                            ToastUtils.showToast("失败");
                            return;
                        }

                        ACacheUtils.setToken(response.body().getData().getToken());
                        RetrofitUtils.getRetrofit(TransportInputDetailsActivity.this, getString(R.string.developer_linhao), getString(R.string.developer_maoyu), getString(R.string.transport_info_module_transport_info), "保存提交")
                                .create(TransportInputApiService.class)
                                .saveOrUpdate(co)
                                .enqueue(new InossemNewsCallback<BaseObject<String>>() {
                                    @Override
                                    public void success(Response<BaseObject<String>> response) {
                                        DialogUtils.getShowMsgDialog(TransportInputDetailsActivity.this, getString(R.string.common_submit_success) + response.body().getData(), () -> {
                                            GreenDaoManager.getInstance().deleteAllotInBean(transportHead.getStockTransportCode());
                                            setResult(999);
                                            finish();
                                        }).show();
                                    }
                                });
                    }

                    @Override
                    public void failure(Response<BaseObject<LogInBean>> response) {
                        ToastUtils.showToast(response.body().getReturnMsg());
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == STATUS_REQUESTCODE_1) {
                List<LocalMedia> list = PictureSelector.obtainMultipleResult(data);
                List<LogFile> submitLogFiles = new ArrayList<>();
                for (int i = STATUS_ZERO; i < list.size(); i++) {
                    LocalMedia localMedia = list.get(i);
                    File file = new File(localMedia.getCompressPath());
                    RequestBody requestFile = RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file);
                    MultipartBody.Part part = MultipartBody.Part.createFormData(FILE, file.getName(), requestFile);
                    RetrofitUtils.getRetrofit(TransportInputDetailsActivity.this, getString(R.string.developer_zhanjianyu), getString(R.string.developer_caideqiang), getString(R.string.input_module_accessory), getString(R.string.input_module_upload_attachment))
                            .create(TransportInputApiService.class)
                            .upload(part)
                            .enqueue(new InossemNewsCallback<BaseObject<LogFile>>() {
                                @Override
                                public void success(Response<BaseObject<LogFile>> response) {
                                    LogFile logFile = response.body().getData();
                                    submitLogFiles.add(logFile);
                                    if (submitLogFiles.size() == list.size()) {
                                        submitFileList = Utils.changeLogFileToBizReceiptAttachmentVo(submitLogFiles);
                                        showPicsToSubmitDialog();
                                    }
                                }
                            });
                }
            }
        }
    }

    private void showPicsToSubmitDialog() {
        if (null != lv && null != submitFileList) {
            SubmitDialogPicAdapter submitDialogPicAdapter = new SubmitDialogPicAdapter(TransportInputDetailsActivity.this, submitFileList);
            lv.setAdapter(submitDialogPicAdapter);
        }
        if (null == submitFileList || submitFileList.isEmpty()) {
            if (iv != null) {
                iv.setVisibility(VISIBLE);
            }
        } else {
            if (iv != null) {
                iv.setVisibility(GONE);
            }
        }
    }

    /**
     * 保存离线单据 只有草稿状态时 才保存
     */
    private void saveOffLineData(TransportItemVo bean) {
        transportHead.setInfo(bean);
        transportHead.setEdit(true);
        AllotInBean allotInBean = new AllotInBean();
        allotInBean.setStockTransportCode(transportHead.getStockTransportCode());
        allotInBean.setCreateTime(transportHead.getCreateTime());
        allotInBean.setMTransportHead(transportHead);
        GreenDaoManager.getInstance().addAllotInBean(allotInBean, true);
    }

    public interface OnButtonClickListener {
        /**
         * item 点击事件
         *
         * @param list 照片
         */
        void onButtonClick(List<LocalMedia> list);
    }

    private class InitBundle {
        private TransportItemVo response;
        private TransportItem vo;
        private ArrayList<String> list;
        private ArrayList<Fragment> listFragment;
        private boolean isCompile;

        InitBundle(TransportItemVo responses) {
            this.response = responses;
        }

        public TransportItemVo getResponse() {
            return response;
        }

        public TransportItem getVo() {
            return vo;
        }

        public ArrayList<String> getList() {
            return list;
        }

        ArrayList<Fragment> getListFragment() {
            return listFragment;
        }

        public boolean isCompile() {
            return isCompile;
        }

        InitBundle invoke() {
            List<TransportItem> listResponseItem = response.getItemList();
            vo = listResponseItem.get(STATUS_ZERO);
            String strResponse = gson.toJson(response);
            list = new ArrayList<>();
            listFragment = new ArrayList<>();
            isCompile = getString(R.string.common_draft).equals(vo.getReceiptStatusName()) || getString(R.string.common_create).equals(vo.getReceiptStatusName()) || getString(R.string.common_have_reject).equals(vo.getReceiptStatusName()) || TextUtils.isEmpty(vo.getReceiptStatusName());
            bundle.putInt(RECEIPT_TYPE, PURCHASE_INSPECTION);
            bundle.putBoolean(CAN_EDIT, isCompile);
            bundle.putString(RESPONSE, strResponse);
            bundle.putString(RECEIPT_CODE, vo.getStockTransportCode());
            return this;
        }
    }
}